/**
 * @author Tin Pham / 7inpham@gmail.com
 */
 
Tinz.GardenWorld = function(assets) {
	THREE.Scene.call(this);
	var that = this;
	var heightData = that.getHeightData(assets[3]);
	console.log(heightData);
	var geometry = new THREE.PlaneGeometry(100, 100, 127, 127);
	var texture = assets[3];
	var material = new THREE.MeshPhongMaterial({
		map: texture,
		ambient: 0xaaaaaa,
		specular: 0xffffff,
		shininess: 0,
		shading: THREE.SmoothShading
	});
	var material = new THREE.MeshBasicMaterial({
		wireframe: true,
		color: 0x0033ff
	});
	this.plane = new THREE.Mesh(geometry, material);
	for (var i = 0; i < this.plane.geometry.vertices.length; i++) {
		this.plane.geometry.vertices[i].z = heightData[i]; // the plane is standing up so modify z instead of y. Rotate it then.
	}
	this.plane.scale.x = this.plane.scale.y = this.plane.scale.z = 30;
	this.plane.position.x = 0;
	this.plane.position.y = -500;
	this.plane.position.z = 0;
	this.plane.rotation.x = - Math.PI/2; // -1.57
	this.plane.rotation.y = 0;
	this.plane.rotation.z = 0;
	this.plane.overdraw = true;
	this.plane.doubleSided = false;
	this.plane.updateMatrix();
	that.add(this.plane);
	// LIGHTS
	var ambient = new THREE.AmbientLight(0xeeeeee);
	this.add(ambient);
	var hemiLight = new THREE.HemisphereLight(0xffffff, 0xffffff, 0.6);
	hemiLight.color.setHSL(0.6, 0.75, 0.5);
	hemiLight.groundColor.setHSL(0.095, 0.5, 0.5);
	hemiLight.position.set(0, 0, 1);
	this.add(hemiLight);

	this.fog = new THREE.Fog(0x34583e, 0, 10000);
};

Tinz.GardenWorld.prototype = Object.create(THREE.Scene.prototype);

/**
 * This function returns an array with all the pixels of an image converted into a value.
 * If the scale value is set to 1, you will get values from 1 (black) to 63,75 (white).
 */
Tinz.GardenWorld.prototype.getHeightData = function(img) {
	var canvas = document.createElement('canvas');
	canvas.width = 128;
	canvas.height = 128;
	var context = canvas.getContext('2d');

	var size = 128 * 128;
	var data = new Float32Array(size);

	context.drawImage(img, 0, 0);

	for (var i = 0; i < size; i++) {
		data[i] = 0;
	}

	var imgd = context.getImageData(0, 0, 128, 128);
	var pix = imgd.data;

	var j = 0;
	for (var i = 0, n = pix.length; i < n; i += (4)) {
		var all = pix[i] + pix[i + 1] + pix[i + 2];
		data[j++] = all / 30; //data[j++] = all/(12*scale);
	}

	return data;
};

Tinz.GardenWorld.prototype.getHeightAt = function(x, z) {
	// get heightMap dimensions
	var width = this.heightMap.length;
	var depth = this.heightMap[0].length;
	// sanity check - boundaries
	console.assert(x >= 0 && x < width);
	console.assert(z >= 0 && z < depth);

	// get the delta within a single segment
	var deltaX = x - Math.floor(x)
	var deltaZ = z - Math.floor(z)

	// get the height of each corner of the segment
	var heightNW = this.heightMap[Math.floor(x)][Math.floor(z)]
	var heightNE = this.heightMap[Math.ceil(x)][Math.floor(z)]
	var heightSW = this.heightMap[Math.floor(x)][Math.ceil(z)]
	var heightSE = this.heightMap[Math.ceil(x)][Math.ceil(z)]

	// test in which triangle the point is. north-east or south-west
	var inTriangleNE = deltaX > deltaZ ? true : false
	if (inTriangleNE) {
		var height = heightNE + (heightNW - heightNE) * (1 - deltaX) + (heightSE - heightNE) * deltaZ
	} else {
		var height = heightSW + (heightSE - heightSW) * deltaX + (heightNW - heightSW) * (1 - deltaZ)
	}
	// return the height
	return height
};
